<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("lib/my_library.php");
require_once("Writer.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtpagecontrol.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class ReminderList extends Page
{
       public $btnExport = null;
       public $JTReminderHidden = null;
       public $JTJavaScript1 = null;
       public $hiddenStyle = null;
       public $JTRecurringTask = null;
       public $imageAddReminder = null;
       public $hiddenVeh = null;
       public $JTReminderList = null;
       public $JTPageControl1 = null;
       public $JTSiteTheme1 = null;
       function btnExportJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel.png"
       <?php

       }

       function btnExportJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel2.png"
       <?php

       }

       function btnExportClick($sender, $params)
       {
       $this->JTReminderHidden->exportGridToXLSDownload('reminder_list.xls');


       }

       function JTReminderHiddenShow($sender, $params)
       {
       $my_lib = new my_library();
       $query1 = "select * from veh_reminder_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());

       $query_od = "select * from vehicle_tbl where company_id='".$_SESSION['company_id']."' and vehicle_id='".$this->hiddenVeh->getValue()."'";
       $result_od = mysql_query($query_od)or die(mysql_error());
       $row_od = mysql_fetch_array($result_od);


       $arr = array();

       $arr[] = array(
                        '1' => 'Vehicle Num:'.' '.$row_od['VIN'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',

                    );


       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row_od['veh_num'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',

                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row_od['year'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',

                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row_od['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',

                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row_od['model_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',

                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row_od['style_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',

                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',

                    );


        $arr[] = array(
      '1' => 'Status',
      '2'=> 'Due at(km/hrs)',
      '3' => 'Due by',
      '4' => 'Mileage(km)',
      '5' => 'Type',
      '6' => 'Reminder Description'
      );



        while($row = mysql_fetch_array($result))
          {
            $vehicle_id = $row['vehicle_id'];
            $status = $row['status'];

            $due_by = "<a href=veh_reminder_edit.php?veh=".$vehicle_id."&reminder=".$row['reminder_id']." title='edit vehicle reminder'>".$row['due_hours']."</a>";
            $tools = "<img onclick=js_deleteReminder('".$vehicle_id."','".$row['reminder_id']."'); src='image/delete_orange.jpg' title='delete'>";
            //"<a href=veh_reminder_delete.php?veh=".$vehicle_id."&reminder=".$row['reminder_id']."><img src='image/delete_orange.jpg' title='delete' no border></a>";

            $due_date = $row['due_date'];
            $curr_date = date("Y-m-d");
            $odometer = $row['due_hours'];
            $curr_od = $row_od['curr_tracking_value'];
            $track_by = $row['schedule_by'];
            $prev_od = $row_od['tracking_value'];

            if($due_date!=null || $due_date!='')
            $days_diff = $my_lib->daysDifference($due_date, $curr_date);

            $odometer_diff = $odometer - $curr_od;
            $mileage = $curr_od - $prev_od;

            if($track_by=='Both')
            {
               if($days_diff>14 && $odometer_diff>1000)
               $gauge = " ";
               if(($days_diff<=14 && $days_diff>7) || ($odometer_diff<=1000 && $odometer_diff>250))
               $gauge = "warning!";
               if($days_diff<=7 || $odometer_diff<=250)
               $gauge = "due!";
            }

            if($track_by=='Hours/Km' || $track_by=='Hours')
            {
               if($odometer_diff>1000)
               $gauge = " ";
               if($odometer_diff<=1000 && $odometer_diff>250)
               $gauge = "warning!";
               if($odometer_diff<=250)
               $gauge = "due!";
            }

            if($track_by=='Date')
            {
               if($days_diff>14)
               $gauge = " ";
               if($days_diff<=14 && $days_diff>7)
               $gauge = "warning!";
               if($days_diff<=7)
               $gauge = "due!";
            }

            $arr[] = array(
                        '1' => $gauge,
                        '2' => $row['due_hours'],
                        '3' => $row['due_date'],
                        '4' => $mileage,
                        '5' => $row['type'],
                        '6' => $row['description']
                        );


          }

            $this->JTReminderHidden->setCellData($arr);


       }


       function JTRecurringTaskShow($sender, $params)
       {
       $query_recurring ="select * from template_recurring_tbl where style_id='".$this->hiddenStyle->getValue()."'";
       $result_recurring = mysql_query($query_recurring)or die(mysql_error());

       $arr = array();
       while($row_recurring = mysql_fetch_array($result_recurring))
          {
            $vehicle_id = $row['vehicle_id'];

            //$type = "<a href=veh_reminder_edit.php?veh=".$vehicle_id."&reminder=".$row['reminder_id'].">".$row['due_hours']."</a>";

            $arr[] = array(
                        'Type' => $row_recurring['type'],
                        'Task Description' => $row_recurring['description'],
                        'Km' => $row_recurring['due_usage'],
                        'Days/Months' => $row_recurring['due_time'].''.$row_recurring['due_by'],
                        );


          }

            $this->JTRecurringTask->setCellData($arr);


       }

       function imageAddReminderJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddReminder'].src="image/add_new.png"
       <?php

       }

       function imageAddReminderJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddReminder'].src="image/add_new_blue.png"
       <?php

       }

       function ReminderListCreate($sender, $params)
       {
       $veh=$_GET['veh'];
       $this->hiddenVeh->setValue($veh);
       $this->imageAddReminder->setLink('veh_reminder_create.php?veh='.$this->hiddenVeh->getValue());
       $style = $_GET['style'];
       $this->hiddenStyle->setValue($style);

       }

       function JTReminderListShow($sender, $params)
       {
       $my_lib = new my_library();
       $query1 = "select * from veh_reminder_tbl where vehicle_id='".$this->hiddenVeh->getValue()."' order by status2 DESC";
       $result = mysql_query($query1)or die(mysql_error());

       $query_od = "select * from vehicle_tbl where company_id='".$_SESSION['company_id']."' and vehicle_id='".$this->hiddenVeh->getValue()."'";
       $result_od = mysql_query($query_od)or die(mysql_error());
       $row_od = mysql_fetch_array($result_od);

        $arr = array();
        while($row = mysql_fetch_array($result))
          {
            $vehicle_id = $row['vehicle_id'];
            $status = $row['status'];
            $status2 = $row['status2'];

            $due_by = "<a href=veh_reminder_edit.php?veh=".$vehicle_id."&reminder=".$row['reminder_id']." title='edit vehicle reminder'>".$row['due_hours']."</a>";
            $tools = "<img onclick=js_deleteReminder('".$vehicle_id."','".$row['reminder_id']."'); src='image/delete.png' title='delete'>";
            //"<a href=veh_reminder_delete.php?veh=".$vehicle_id."&reminder=".$row['reminder_id']."><img src='image/delete_orange.jpg' title='delete' no border></a>";

            $due_date = $row['due_date'];
            $curr_date = date("Y-m-d");
            $odometer = $row['due_hours'];
            $curr_od = $row_od['curr_tracking_value'];
            $curr_hr = $row_od['curr_hours'];
            $track_by = $row['schedule_by'];
            $prev_od = $row_od['tracking_value'];
            $prev_hr = $row_od['start_hours'];

            if($due_date!=null || $due_date!='')
            $days_diff = $my_lib->daysDifference($due_date, $curr_date);

            $veh_track = $row_od['track_by'];

            if($veh_track=='distance')
            {
            $odometer_diff = $odometer - $curr_od;
            $mileage = $curr_od - $prev_od;
            }

            if($veh_track=='hours')
            {
            $odometer_diff = $odometer - $curr_hr;
            $mileage = $curr_od - $prev_hr;
            }


            if($track_by=='Both')
            {
               if($days_diff>14 && $odometer_diff>1000)
               $gauge = "<img src='image/gauge_green.png' border=0/>";
               if(($days_diff<=14 && $days_diff>7) || ($odometer_diff<=1000 && $odometer_diff>250))
               $gauge = "<img src='image/gauge_yellow.png' border=0/>";
               if($days_diff<=7 || $odometer_diff<=250)
               $gauge = "<img src='image/gauge_red.png' border=0/>";
            }

            if($track_by=='Hours/Km' || $track_by=='Hours'|| $track_by=='Mileage')
            {
               if($odometer_diff>1000)
               $gauge = "<img src='image/gauge_green.png' border=0/>";
               if($odometer_diff<=1000 && $odometer_diff>250)
               $gauge = "<img src='image/gauge_yellow.png' border=0/>";
               if($odometer_diff<=250)
               $gauge = "<img src='image/gauge_red.png' border=0/>";
            }

            if($track_by=='Date')
            {
               if($days_diff>14)
               $gauge = "<img src='image/gauge_green.png' border=0/>";
               if($days_diff<=14 && $days_diff>7)
               $gauge = "<img src='image/gauge_yellow.png' border=0/>";
               if($days_diff<=7)
               $gauge = "<img src='image/gauge_red.png' border=0/>";
            }

            $sched_list = $row['schedule_by'];
            if($sched_list=='Date')
            {
            $due = "<a href=veh_reminder_edit.php?veh=".$vehicle_id."&reminder=".$row['reminder_id']." title='edit vehicle reminder'>".$row['due_date']."</a>";
            }
            if($sched_list=='Hours')
            {
            $due = "<a href=veh_reminder_edit.php?veh=".$vehicle_id."&reminder=".$row['reminder_id']." title='edit vehicle reminder'>".$row['due_hours'].' '.'hours'."</a>";
            }
            if($sched_list=='Mileage')
            {
            $due = "<a href=veh_reminder_edit.php?veh=".$vehicle_id."&reminder=".$row['reminder_id']." title='edit vehicle reminder'>".$row['due_hours'].' '.'Km'."</a>";
            }

            if($status2=="Complete")
            {
            $color = "Silver";
            }

            if($status2=="Pending")
            {
            $color = "Black";
            }

            $arr[] = array(
                        'Status' => "<p style='color:".$color."'>".$gauge."</p>",
                        //'Scheduled by' => $row['schedule_by'],
                        'Due' => "<p style='color:".$color."'>".$due."</p>",
                        //'Due by' => $row['due_date'],
                        'Mileage(km)' => "<p style='color:".$color."'>".$mileage."</p>",
                        'Type' => "<p style='color:".$color."'>".$row['type']."</p>",
                        'Reminder Description' => "<p style='color:".$color."'>".$row['description']."</p>",
                        'Tools' => $tools
                        );


          }

            $this->JTReminderList->setCellData($arr);



       }

}

global $application;

global $ReminderList;

//Creates the form
$ReminderList=new ReminderList($application);

//Read from resource file
$ReminderList->loadResource(__FILE__);

//Shows the form
$ReminderList->show();

?>